/**
 * Alipay.com Inc.
 * Copyright (c) 2004-2022 All Rights Reserved.
 */
package com.example.spider.proxy.provider.ip3366;

import cn.hutool.core.util.ReUtil;
import com.example.spider.proxy.core.ProxyInfo;
import com.example.spider.proxy.core.tester.ProxyTypeEnum;
import com.example.spider.proxy.provider.AbstractProxyResultHandler;
import com.example.spider.proxy.provider.IpProxySpiderTask;
import com.example.spider.starter.SpiderStarter;
import com.example.spider.task.SpiderTaskQueue;
import org.shoulder.core.converter.LocalDateTimeConverter;

import java.nio.charset.Charset;
import java.util.List;

/**
 * 齐云代理
 * 历史网站 www.7yip.cn/free
 * http://www.ip3366.net/free/  只能看第一页，15条，24 小时更新一次 【只有一页，后面翻页内容不变】
 *
 * @author lym
 */
//@Component
public class Ip3366ResultHandler extends AbstractProxyResultHandler implements SpiderStarter {

    public Ip3366ResultHandler() {
        super("ip3366");
    }

    protected IpProxySpiderTask customizeTask(IpProxySpiderTask task) {
        task.setCharset(Charset.forName("gb2312"));
        return task;
    }

    @Override
    public void startSpiders() {
        // 不限地区
        IpProxySpiderTask task = createTask("http://www.ip3366.net/free/");
        SpiderTaskQueue.putTask(task);
    }


    @Override
    public String executeCron() {
        return "0 0 */2 * * ?";
    }

    @Override
    public void doHandle(IpProxySpiderTask task, String html) {

        String regex = " <tr>\\s*?<td>(.*?)</td>" +
                "\\s*?<td>(.*?)</td>" +
                // 高匿代理IP
                "\\s*?<td>(.*?)</td>" +
                "\\s*?<td>(.*?)</td>" +
                "\\s*?<td>(.*?)</td>" +
                "\\s*?<td>(.*?)秒</td>" +
                "\\s*?<td>(.*?)</td>" +
                "\\s*?</tr>";

        List<String> ipList = ReUtil.findAll(regex, html, 1);
        List<String> portList = ReUtil.findAll(regex, html, 2);
        List<String> anonymityList = ReUtil.findAll(regex, html, 3);
        List<String> typeList = ReUtil.findAll(regex, html, 4);
        List<String> locationList = ReUtil.findAll(regex, html, 5);
        List<String> responseTimeList = ReUtil.findAll(regex, html, 6);
        List<String> lastVerificationTimeList = ReUtil.findAll(regex, html, 7);

        for (int i = 0; i < ipList.size(); i++) {
            ProxyInfo proxyInfo = ProxyInfo.builder()
                    .source(taskName)
                    .ip(ipList.get(i))
                    .port(Integer.parseInt(portList.get(i)))
                    .anonymity(anonymityList.get(i).contains("高匿") ? "高匿" : "普通")
                    .proxyTypeEnum(ProxyTypeEnum.of(typeList.get(i)))
                    .type(ProxyTypeEnum.of(typeList.get(i)).getType())
//                    .supportHttps(typeList.get(i).contains("HTTPS"))
                    .location(locationList.get(i))
                    .responseTime(ProxyInfo.parseSecondToMill(responseTimeList.get(i)))
                    .lastVerificationTime(LocalDateTimeConverter.INSTANCE.convert(lastVerificationTimeList.get(i)))
                    .build();

            addProxyToSourceRepository(proxyInfo);
        }
    }

}